package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import com.gc.business.domain.CommunityManage;
import com.gc.business.domain.ElevatorFault;
import com.gc.business.domain.ElevatorInfo;
import com.gc.business.domain.ElevatorRepair;
import com.gc.business.service.ICommunityManageService;
import com.gc.business.service.IElevatorFaultService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.business.service.IElevatorRepairService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯维修 控制层
 * 
 * @author liujinteng
 * @date 2020-11-20
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorRepair")
public class ElevatorRepairController extends BaseController {
    private String prefix = "business/elevatorRepair";

	@Autowired
	private ISysUserService sysUserService;
	
	private final IElevatorRepairService elevatorRepairService;

	private final IElevatorInfoService elevatorInfoService;

	private final IElevatorFaultService elevatorFaultService;

	private final ICommunityManageService communityManageService;

	@Autowired
	public ElevatorRepairController(IElevatorRepairService elevatorRepairService,
									IElevatorInfoService elevatorInfoService,
									IElevatorFaultService elevatorFaultService,
									ICommunityManageService communityManageService) {
		this.elevatorRepairService = elevatorRepairService;
		this.elevatorInfoService = elevatorInfoService;
		this.elevatorFaultService = elevatorFaultService;
		this.communityManageService = communityManageService;
	}

	/**
	*  列表页(待维修)
	*/
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping
	public String elevatorRepair(){
	    return prefix + "/elevatorRepair";
	}

	/**
	 *  详情页(待维修)
	 */
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping("/elevatorRepairInfo/{repairId}")
	public String elevatorRepairInfo(@PathVariable("repairId") Long repairId, ModelMap mmap){
		ElevatorRepair elevatorRepair = elevatorRepairService.getById(repairId);
		mmap.put("elevatorRepair", elevatorRepair);
		ElevatorFault elevatorFault = new ElevatorFault();
		if (StringUtils.isNotEmpty(elevatorRepair.getFaultCode())){
			elevatorFault = elevatorFaultService.findFaultByCode(elevatorRepair.getFaultCode());
			mmap.put("elevatorFault", elevatorFault);
		}else {
			mmap.put("elevatorFault", elevatorFault);
		}
		ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorRepair.getElevatorId());
		mmap.put("elevatorInfo", elevatorInfo);
		return prefix + "/elevatorRepairInfo";
	}

	/**
	 *  列表页(待确认)
	 */
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping("/confirm")
	public String elevatorRepairConfirm(){
		return prefix + "/elevatorRepairConfirm";
	}

	/**
	 *  详情页(待确认)
	 */
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping("/elevatorConfirmInfo/{repairId}")
	public String elevatorConfirmInfo(@PathVariable("repairId") Long repairId, ModelMap mmap){
		ElevatorRepair elevatorRepair = elevatorRepairService.getById(repairId);
		mmap.put("elevatorRepair", elevatorRepair);
		ElevatorFault elevatorFault = new ElevatorFault();
		if (StringUtils.isNotEmpty(elevatorRepair.getFaultCode())){
			elevatorFault = elevatorFaultService.findFaultByCode(elevatorRepair.getFaultCode());
			mmap.put("elevatorFault", elevatorFault);
		}else {
			mmap.put("elevatorFault", elevatorFault);
		}
		ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorRepair.getElevatorId());
		mmap.put("elevatorInfo", elevatorInfo);
		return prefix + "/elevatorConfirmInfo";
	}

	/**
	 *  列表页(已完成)
	 */
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping("/done")
	public String elevatorRepairDone(){
		return prefix + "/elevatorRepairDone";
	}

	/**
	 *  详情页(已完成)
	 */
	@RequiresPermissions("business:elevatorRepair:view")
	@GetMapping("/elevatorDoneInfo/{repairId}")
	public String elevatorDoneInfo(@PathVariable("repairId") Long repairId, ModelMap mmap){
		ElevatorRepair elevatorRepair = elevatorRepairService.getById(repairId);
		mmap.put("elevatorRepair", elevatorRepair);
		ElevatorFault elevatorFault = new ElevatorFault();
		if (StringUtils.isNotEmpty(elevatorRepair.getFaultCode())){
			elevatorFault = elevatorFaultService.findFaultByCode(elevatorRepair.getFaultCode());
			mmap.put("elevatorFault", elevatorFault);
		}else {
			mmap.put("elevatorFault", elevatorFault);
		}
		ElevatorInfo elevatorInfo = elevatorInfoService.getById(elevatorRepair.getElevatorId());
		mmap.put("elevatorInfo", elevatorInfo);
		return prefix + "/elevatorDoneInfo";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{repairId}")
	public String edit(@PathVariable("repairId") Long repairId, ModelMap mmap){
		ElevatorRepair elevatorRepair = elevatorRepairService.getById(repairId);
		mmap.put("elevatorRepair", elevatorRepair);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯维修列表
	 */
	@RequiresPermissions("business:elevatorRepair:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Long reviewType, String isOverhaul,ElevatorRepair elevatorRepair) {
		List<ElevatorRepair> elevatorRepairList = new ArrayList<>();
		SysUser sysUser = sysUserService.selectUserById(ShiroUtils.getUserId());
		//查询用户角色列
		String userRoles = sysUserService.selectUserRole(ShiroUtils.getUserId());
		//判断是否为维修经理
		if(reviewType == 2L && userRoles.contains(BusinessConstants.KEEP_MANAGER)){
			startPage();
			elevatorRepairList = elevatorRepairService.selectRepairList(elevatorRepair,reviewType,sysUser.getAttrId(),isOverhaul);
		}else{
			startPage();
			elevatorRepairList = elevatorRepairService.selectRepairList(elevatorRepair,reviewType,sysUser.getAttrId(),isOverhaul);
		}
		return Result.success(getDataTable(elevatorRepairList));
	}

	/**
	 * 新增保存电梯维修
	 */
	@RequiresPermissions("business:elevatorRepair:add")
	@Log(title = "电梯维修", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ElevatorRepair elevatorRepair){
		elevatorRepairService.save(elevatorRepair);
		return Result.success();
	}

	/**
	 * 修改保存电梯维修
	 */
	@RequiresPermissions("business:elevatorRepair:edit")
	@Log(title = "电梯维修", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ElevatorRepair elevatorRepair){
		elevatorRepairService.updateById(elevatorRepair);
		return Result.success();
	}
	
	/**
	 * 删除电梯维修
	 */
	@RequiresPermissions("business:elevatorRepair:remove")
	@Log(title = "电梯维修", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		elevatorRepairService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯维修列表
	 */
	@RequiresPermissions("business:elevatorRepair:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ElevatorRepair elevatorRepair){
		List<CommunityManage> communityList = sysUserService.findCommunityByUserId(ShiroUtils.getUserId());
		if (CollUtil.isEmpty(communityList)){
			return Result.success();
		}
		List<Long> collect = communityList.stream().map(CommunityManage::getCommunityId).collect(Collectors.toList());
		List<ElevatorInfo> elevatorInfoList = elevatorInfoService.list(new ElevatorInfo(), collect);
		if (CollUtil.isEmpty(elevatorInfoList) || elevatorInfoList.size() == 0) {
			return Result.success();
		}
		List<Long> collect1 = elevatorInfoList.stream().map(ElevatorInfo::getElevatorId).collect(Collectors.toList());
		List<ElevatorRepair> list = elevatorRepairService.list(elevatorRepair,collect1);
		ExcelUtil<ElevatorRepair> util = new ExcelUtil<>(ElevatorRepair.class);
		util.exportExcel(list, "elevatorRepair");
		return Result.success();
	}
}
